package com.sxkf.core.third.sms;

import com.alibaba.fastjson.JSON;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.SxkfException;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Component// 标示一个Spring框架的服务组件 // 使用Lombok的日志注解，方便记录日志
public class ALISMSUtils {
    private final String accessKeyId = "LTAI5t87WNX9bzwX2Qz8QaRA";

    private final String accessKeySecret = "ocuytfEGHhN0te5pY8u6xQ0hEOmLSW";

    private final String signName = "四川莫大帅科技";


    /**
     * 发送短信验证码
     *
     * @param phone 目标手机号码
     * @param code  验证码内容
     */
    public void sendSMS(String phone, String code) {
        // 使用静态凭证提供者初始化凭证
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        // 配置阿里云SMS客户端
        AsyncClient client = AsyncClient.builder()
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com") // 设置端点覆盖
                )
                .build();

        // 准备发送的短信参数
        Map<String,String> templateParam = new HashMap<>(); // 设置模板参数，如验证码
        templateParam.put("code", code);
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(phone) // 目标手机号
                .signName(signName) // 短信签名
                .templateCode("SMS_308695616") // 短信模板代码，此处为空，需根据实际情况填写
                .templateParam(JSON.toJSONString(templateParam)) // 模板参数序列化
                .build();

        // 异步发送短信并处理响应
        try {
            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            SendSmsResponse resp = response.get(); // 等待异步任务完成获取响应
            log.info(new Gson().toJson(resp)); // 记录发送结果到日志
            if (!Objects.equals("OK",resp.getBody().getCode())){
                throw new SxkfException("发送短信异常："+resp.getBody().getMessage());
            }
        } catch (Exception e) {
            log.error("发送短信失败", e);
            throw new SxkfException("发送短信失败："+e.getMessage());
        }finally {
            client.close(); // 关闭客户端资源
        }
    }

    public boolean sendSMS(String phone, String templateCode, String templateParam) {
//        if (environmentUtils.isDevEnvironment()) {
//            log.info("手机号：" + phone + "，内容：" + templateParam);
//            return true;
//        }
        // 使用静态凭证提供者初始化凭证
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        // 配置阿里云SMS客户端
        AsyncClient client = AsyncClient.builder()
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com") // 设置端点覆盖
                )
                .build();
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(phone) // 目标手机号
                .signName(signName) // 短信签名
                .templateCode(templateCode) // 短信模板代码，此处为空，需根据实际情况填写
                .templateParam(templateParam) // 模板参数序列化
                .build();

        // 异步发送短信并处理响应
        try {
            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            SendSmsResponse resp = response.get(); // 等待异步任务完成获取响应
            log.info(new Gson().toJson(resp)); // 记录发送结果到日志
            client.close(); // 关闭客户端资源
            return true;
        } catch (Exception e) {
            client.close();
            return false;
        }
    }

}

